import FileSaver from 'file-saver'
import XLSX from 'xlsx'
import moment from 'moment'
function exportExcel(domId, filename = '导出数据') {
  // 调用生成excel表格并下载的方法
  /* generate workbook object from table */
  var xlsxParam = { raw: true } // 导出的内容只做解析，不进行格式转换

  var wb = XLSX.utils.table_to_book(
    // 找到表格DOM对象
    document.getElementById(domId),
    // this.$refs.exportTab,
    xlsxParam
  )
  /* get binary string as output */
  var wbout = XLSX.write(wb, {
    bookType: 'xlsx',
    bookSST: true,
    type: 'array'
  })
  try {
    FileSaver.saveAs(
      new Blob([wbout], { type: 'application/octet-stream' }),
      //  商品数据导出-20221130114123
      //   filename + '-' + parseTime() + '.xlsx',
      filename + '-' + moment().format('YYMMDDHHmmss') + '.xlsx'
    )
  } catch (e) {
    if (typeof console !== 'undefined') {
      console.log(e, wbout)
    }
  }
  return wbout
}
function parseTime() {
  // js中如何获取格式化的时间  年月日 时分秒
  const myDate = new Date()
  let year = myDate.getFullYear()
  let month = myDate.getMonth() + 1
  let day = myDate.getDate()
  let hours = myDate.getHours()
  let minutes = myDate.getMinutes()
  let seconds = myDate.getSeconds()
  //   console.log(year, month, day, hours, minutes, seconds)
  return '' + year + month + day + hours + minutes + seconds
}
parseTime()
export default exportExcel
